package com.sx.manage.listener;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.sx.manage.service.bank.IMBasePayBankService;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.manage.mybatis.form.bank.MPayBankForm;
import com.sx.model.manage.mybatis.vo.bank.MPayBankVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;

/**
 * 系统初始化监听器
 * @author zzl
 *
 */
@Service("cacheListener")  	
public class MCacheListener implements ApplicationListener<ContextRefreshedEvent> {
	
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	//系统参数存储集合
	private   static Map<String,String> peoperties = new HashMap<>();
	//总行信息
	private   static Map<String,MPayBankVO> provinceMap = new HashMap<>();
	
	//银行总行信息
	@Resource(name="mBasePayBankServiceImpl")
	private IMBasePayBankService mBasePayBankServiceImpl;
	
    public static Map<String, MPayBankVO> getProvinceMap() {
		return provinceMap;
	}
	public static void setProvinceMap(Map<String, MPayBankVO> provinceMap) {
		MCacheListener.provinceMap = provinceMap;
	}
	public void onApplicationEvent(ContextRefreshedEvent evt) {
    	_log.info(" spring 启动完成，缓存数据加载");
    	//加载配置文件
		loadConfigure();
		//加载总行
		provinceLoad();
    }
	//加载总行
	public void provinceLoad(){
		ResultListVO<MPayBankVO> result = mBasePayBankServiceImpl.query_list(new MPayBankForm());
		if( null != result){
			List<MPayBankVO> voList= result.getData();
			voList.forEach( (MPayBankVO vo)->{
				provinceMap.put(vo.getBank_name(), vo);
			});
		}
	}
	
	//加载配置文件
    public void loadConfigure(){
		  InputStream inStearm=MCacheListener.class.getClassLoader().getResourceAsStream(MSystemConstants.XML_APPLICATION_PATH);
	      // 流转对象  
	       Properties pro = new Properties();  
	       try {  
	    	   pro.load(inStearm);  
	       } catch (Exception e) {  
	           _log.error("加载application文件时出错",e);
	       }  
	  
	    Enumeration<?> enumer=pro.keys();
	    while(enumer.hasMoreElements()){
	      Object object=enumer.nextElement();
	      peoperties.put(object.toString(), pro.getProperty(object.toString()));
	    }
	}
	
	public Logger get_log() {
		return _log;
	}
	public void set_log(Logger _log) {
		this._log = _log;
	}
	public static Map<String, String> getPeoperties() {
		return peoperties;
	}
	public static void setPeoperties(Map<String, String> peoperties) {
		MCacheListener.peoperties = peoperties;
	}
  
}